package com.hsnn.medstgmini.base.dict.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsnn.medstgmini.base.dict.service.DicPackunitLogManager;
import com.hsnn.medstgmini.util.Pagination;

/**
 * @category 包装单位字典维护记录
 * @author 王炳
 * @date 2015年7月27日
 */
@Controller
@RequestMapping("/dicPackunitLog")
public class DicPackunitLogController {
	
	private static final Logger log = Logger.getLogger(DicPackunitController.class);
			
	
	@Autowired
	private DicPackunitLogManager dicPackunitLogManager;

	
	@Autowired
	private HttpServletRequest request;
	
	/**
	 * 
	 *@category 跳转包装单位字典维护记录页面
	 *@author wangbing
	 * @throws UnsupportedEncodingException 
	 *@date 2015年7月27日
	 *String
	 */
	@RequestMapping("/log")
	public String  toDicPackunitLogList() throws UnsupportedEncodingException {
		printParams();
		request.setAttribute("packunitId", request.getParameter("packunitId"));
		request.setAttribute("packunitCode", request.getParameter("packunitCode"));
		String packunitName = new String(request.getParameter("packunitName").getBytes("ISO8859-1"),"utf-8");
		request.setAttribute("packunitName", packunitName);
		return "/base/dict/packunitLogList";
	}
	
	/**
	 * 
	 *@category 获取包装单位字典维护记录列表
	 *@author wangbing
	 *@date 2015年7月27日
	 *String
	 */
	@RequestMapping("/getDicPackunitLogList")
	@ResponseBody
	public Pagination  getDicPackunitLogList(){
		printParams();
		Pagination  page = new Pagination(request);
		page.getConditions().put("startDate", request.getParameter("date1"));
		page.getConditions().put("endDate", request.getParameter("date2"));
		page.getConditions().put("packunitId", request.getParameter("packunitId"));
		try {
			dicPackunitLogManager.getDicPackunitLogList(page);
			page.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			page.setSuccess(false);
		}
		return page;
	}
	
	/**
	 * 
	 *@category 跳转包装单位字典维护记录详情页面
	 *@author 徐国鹏
	 *@date 2015年7月27日
	 *String
	 */
	@RequestMapping("/toDicPackunitLogDetail")
	public String  toDicPackunitLogDetail() {
		printParams();
		try {
			Pagination  page = new Pagination();
			page.getConditions().put("logId", request.getParameter("logId"));
			List<Map<String,Object>> list = dicPackunitLogManager.getDicPackunitLogDetail(page);
			List<Map<String,Object>> dataList = new ArrayList<Map<String,Object>>();
			List<Map<String,Object>> mapList = new ArrayList<Map<String,Object>>();
			String[] cColunm = {"包装单位编码","包装单位名称","状态"};
			String[] eColunm = {"packunitCode","packunitName","status"};
			for(int i = 0;i<cColunm.length;i++){
				Map<String,Object> map =  new HashMap<String,Object>();
				map.put("colunm", cColunm[i]);//字段名
				map.put("after", !"status".equals(eColunm[i])?list.get(0).get(eColunm[i]):"1".equals(list.get(0).get(eColunm[i]).toString())?"启用":"停用");//修改后数据
				map.put("before", list.size()==1?null:!"status".equals(eColunm[i])?list.get(1).get(eColunm[i]):"1".equals(list.get(1).get(eColunm[i]).toString())?"启用":"停用");//修改前数据
				if(list.size()==1){
					map.put("red", "1");//是否变红加粗 1是 0 否
				}else{
					//判断修改后与修改前数据是否相等
					if(list.get(0).get(eColunm[i])!=null&&list.get(1).get(eColunm[i])!=null&&!list.get(0).get(eColunm[i]).toString().equals(list.get(1).get(eColunm[i]).toString())){
						map.put("red", "1");
					}else{
						map.put("red", "0");
					}
				}
				mapList.add(map);
			}
			for(int i = 0;i<mapList.size();i++){//把要变红的放在list前面
				if("1".equals(mapList.get(i).get("red"))){
					dataList.add(mapList.get(i));
				}
			}
			for(int i = 0;i<mapList.size();i++){
				if("0".equals(mapList.get(i).get("red"))){
					dataList.add(mapList.get(i));
				}
			}
			request.setAttribute("list", list);
			request.setAttribute("dataList", dataList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		/*log.info("enter toDicPackunitLogDetail method!");
		Pagination  page = new Pagination();
		page.getConditions().put("logId", request.getParameter("logId"));
		List<Map<String,Object>> list = dicPackunitLogManager.getDicPackunitLogDetail(page);
		request.setAttribute("list", list);*/
		return "/base/dict/packunitLogDetail";
	}

	/**
	 * 
	 *@category 获取参数打印
	 *@author 徐国鹏
	 *@date 2015年6月9日
	 *Pagination
	 */
	@SuppressWarnings("rawtypes")
	public void printParams(){
		Enumeration paramNames = request.getParameterNames();
		try {
			while (paramNames.hasMoreElements()) {
				String paramName = (String) paramNames.nextElement();
				String paramValue = request.getParameter(paramName);
				log.info(paramName+":"+paramValue);
			}
		} catch (Exception e) {
			log.error("",e);
		}
	}
	
}
